<%@page import="cn.school.mybatis.service.StudentService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*, cn.school.mybatis.pojo.Student" %>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../js/jquery-3.4.1.min.js"></script>
</head>
<body>
<%-- <%@include file="/checkLogin.jsp" %> --%>
<p align="right">
 <a href="addStudent.jsp">添加新生</a>
</p>
<%

StudentService service=new StudentService();
int pageNo=1;
int pageSize=10;
try{
	pageNo=Integer.parseInt(request.getParameter("p"));
}catch(Exception e){}

Map<String, Object> map = new HashMap<>();
String sname=request.getParameter("sname");
String telephone=request.getParameter("telephone");
sname=sname==null?"":sname;
telephone=telephone==null?"":telephone;
map.put("sname", sname);
map.put("telephone", telephone);

List<Student> list = service.search0(map, pageNo, pageSize, "stuno asc");
int totalPage=service.searchTotal(map, pageSize);

%>
<p id="search">
 <form method="get" action="student.jsp">
  <input name="sname" value="<%=sname%>" placeholder="姓名"/>
  <input name="telephone" value="<%=telephone%>" placeholder="电话号码"/>
  <input type="submit" value="搜索"/>
 </form>
</p>
<table width="80%" border="1" cellspacing="0" cellpadding="0"> 
 <tr>
  <td>学号</td>
  <td>姓名</td>
  <td>性别</td>
  <td>操作</td>
 </tr>
<%

for(Student s:list){
%>
 <tr>
  <td><%=s.getId() %></td>
  <td><%=s.getSname() %></td>
  <td><%=s.getSex()%></td>
  <td>
    <a href="toUpdateStudent.jsp?stuno=<%=s.getId()%>">修改</a>
    <a href="toDelStudent.jsp?stuno=<%=s.getId()%>" onclick="return confirm('真的要删除<%=s.getSname() %>?');">删除</a>
  </td>
 </tr>
<%} %>
</table>
<p align="right" style="margin-right:200px">
 <a href="student.jsp?p=1&sname=<%=sname%>&telephone=<%=telephone%>">首页</a>
 <% if(pageNo>1) {%>
   <a href="student.jsp?p=<%=pageNo-1%>&sname=<%=sname%>&telephone=<%=telephone%>">上一页</a>
 <%} %>
 <input type="number" min="1" max="<%=totalPage%>" value="<%=pageNo%>" name="p" style="width:30px"/>/<%=totalPage%>
 <input type="button" id="go" value="go"/>
 <%
   if(pageNo<totalPage){
 %> 
   <a href="student.jsp?p=<%=pageNo+1%>&sname=<%=sname%>&telephone=<%=telephone%>">下一页</a>
  <%} %>
   <a href="student.jsp?p=<%=totalPage%>&sname=<%=sname%>&telephone=<%=telephone%>">尾页</a>
</p>
<script>
 $(function(){
	 $("#go").click(function(){
		 let p=$(this).prev().val();
		 //p在 1 和 totalPage 之间
		 window.location='student.jsp?p='+p+'&sname=<%=sname%>&telephone=<%=telephone%>';
	 });
 });
</script>
</body>
</html>